added samples
[windows-sources.git] / sdk / samples / all in on code / Visual Studio 2010 / VBWebBrowserWithProxy / WinINet.vb
blob67788adf4f56b8db44be16ac4a1f32415d852051
1 '*************************** Module Header ******************************'
2 ' Module Name: WinINet.vb
3 ' Project: VBWebBrowserWithProxy
4 ' Copyright (c) Microsoft Corporation.
5 '
6 ' This class is a simple .NET wrapper of wininet.dll. It contains 2 extern
7 ' methods (InternetSetOption and InternetQueryOption) of wininet.dll. This
8 ' class can be used to set proxy, disable proxy, backup internet options
9 ' and restore internet options.
11 ' This source is subject to the Microsoft Public License.
12 ' See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
13 ' All other rights reserved.
15 ' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
16 ' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
17 ' WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
18 '*************************************************************************'
20 Imports System.Runtime.InteropServices
22 Public NotInheritable Class WinINet
24 Private Shared _agent As String = Process.GetCurrentProcess().ProcessName
26 ''' <summary>
27 ''' Set the LAN connection proxy server for current process.
28 ''' </summary>
29 ''' <param name="proxyServer">
30 ''' The Proxy Server.
31 ''' </param>
32 ''' <returns></returns>
33 Public Shared Function SetConnectionProxy(ByVal isMachineSetting As Boolean, ByVal proxyServer As String) As Boolean
34 If isMachineSetting Then
35 Return SetConnectionProxy(String.Empty, proxyServer)
36 Else
37 Return SetConnectionProxy(_agent, proxyServer)
38 End If
39 End Function
41 ''' <summary>
42 ''' Set the LAN connection proxy server.
43 ''' </summary>
44 ''' <param name="agentName">
45 ''' If agentName is null or empty, this function will set the Lan proxy for
46 ''' the machine, else for the current process.
47 ''' </param>
48 ''' <param name="proxyServer">The Proxy Server.</param>
49 ''' <returns></returns>
50 Public Shared Function SetConnectionProxy(ByVal agentName As String, ByVal proxyServer As String) As Boolean
51 Dim hInternet As IntPtr = IntPtr.Zero
52 Try
53 If Not String.IsNullOrEmpty(agentName) Then
54 hInternet = NativeMethods.InternetOpen(agentName, CInt(Fix(INTERNET_OPEN_TYPE.INTERNET_OPEN_TYPE_DIRECT)), Nothing, Nothing, 0)
55 End If
57 Return SetConnectionProxyInternal(hInternet, proxyServer)
58 Finally
59 If hInternet <> IntPtr.Zero Then
60 NativeMethods.InternetCloseHandle(hInternet)
61 End If
62 End Try
63 End Function
65 ''' <summary>
66 ''' Set the proxy server for LAN connection.
67 ''' </summary>
68 Shared Function SetConnectionProxyInternal(ByVal hInternet As IntPtr, ByVal proxyServer As String) As Boolean
70 ' Create 3 options.
71 Dim Options(2) As INTERNET_PER_CONN_OPTION
73 ' Set PROXY flags.
74 Options(0) = New INTERNET_PER_CONN_OPTION()
75 Options(0).dwOption = CInt(Fix(INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_FLAGS))
76 Options(0).Value.dwValue = CInt(Fix(INTERNET_OPTION_PER_CONN_FLAGS.PROXY_TYPE_PROXY))
78 ' Set proxy name.
79 Options(1) = New INTERNET_PER_CONN_OPTION()
80 Options(1).dwOption = CInt(Fix(INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_PROXY_SERVER))
81 Options(1).Value.pszValue = Marshal.StringToHGlobalAnsi(proxyServer)
83 ' Set proxy bypass.
84 Options(2) = New INTERNET_PER_CONN_OPTION()
85 Options(2).dwOption = CInt(Fix(INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_PROXY_BYPASS))
86 Options(2).Value.pszValue = Marshal.StringToHGlobalAnsi("local")
88 ' Allocate a block of memory of the options.
89 Dim buffer As System.IntPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(Options(0)) + Marshal.SizeOf(Options(1)) + Marshal.SizeOf(Options(2)))
91 Dim current As System.IntPtr = buffer
93 ' Marshal data from a managed object to an unmanaged block of memory.
94 For i As Integer = 0 To Options.Length - 1
95 Marshal.StructureToPtr(Options(i), current, False)
96 current = CType(CInt(current) + Marshal.SizeOf(Options(i)), System.IntPtr)
97 Next i
99 ' Initialize a INTERNET_PER_CONN_OPTION_LIST instance.
100 Dim option_list As New INTERNET_PER_CONN_OPTION_LIST()
102 ' Point to the allocated memory.
103 option_list.pOptions = buffer
105 ' Return the unmanaged size of an object in bytes.
106 option_list.Size = Marshal.SizeOf(option_list)
108 ' IntPtr.Zero means LAN connection.
109 option_list.Connection = IntPtr.Zero
111 option_list.OptionCount = Options.Length
112 option_list.OptionError = 0
113 Dim size As Integer = Marshal.SizeOf(option_list)
115 ' Allocate memory for the INTERNET_PER_CONN_OPTION_LIST instance.
116 Dim intptrStruct As IntPtr = Marshal.AllocCoTaskMem(size)
118 ' Marshal data from a managed object to an unmanaged block of memory.
119 Marshal.StructureToPtr(option_list, intptrStruct, True)
121 ' Set internet settings.
122 Dim bReturn As Boolean = NativeMethods.InternetSetOption( _
123 hInternet, INTERNET_OPTION.INTERNET_OPTION_PER_CONNECTION_OPTION, intptrStruct, size)
125 ' Free the allocated memory.
126 Marshal.FreeCoTaskMem(buffer)
127 Marshal.FreeCoTaskMem(intptrStruct)
129 ' Throw an exception if this operation failed.
130 If Not bReturn Then
131 Throw New ApplicationException(" Set Internet Option Failed!")
132 End If
134 ' Notify the system that the registry settings have been changed and cause
135 ' the proxy data to be reread from the registry for a handle.
136 NativeMethods.InternetSetOption(hInternet, INTERNET_OPTION.INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0)
138 NativeMethods.InternetSetOption(hInternet, INTERNET_OPTION.INTERNET_OPTION_REFRESH, IntPtr.Zero, 0)
140 Return bReturn
141 End Function
143 ''' <summary>
144 ''' Get the current system options for LAN connection.
145 ''' Make sure free the memory after restoration.
146 ''' </summary>
147 Public Shared Function GetSystemProxy() As INTERNET_PER_CONN_OPTION_LIST
149 ' Query following options.
150 Dim Options(2) As INTERNET_PER_CONN_OPTION
152 Options(0) = New INTERNET_PER_CONN_OPTION()
153 Options(0).dwOption = CInt(Fix(INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_FLAGS))
154 Options(1) = New INTERNET_PER_CONN_OPTION()
155 Options(1).dwOption = CInt(Fix(INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_PROXY_SERVER))
156 Options(2) = New INTERNET_PER_CONN_OPTION()
157 Options(2).dwOption = CInt(Fix(INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_PROXY_BYPASS))
159 ' Allocate a block of memory of the options.
160 Dim buffer As System.IntPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(Options(0)) + Marshal.SizeOf(Options(1)) + Marshal.SizeOf(Options(2)))
162 Dim current As System.IntPtr = CType(buffer, System.IntPtr)
164 ' Marshal data from a managed object to an unmanaged block of memory.
165 For i As Integer = 0 To Options.Length - 1
166 Marshal.StructureToPtr(Options(i), current, False)
167 current = CType(CInt(current) + Marshal.SizeOf(Options(i)), System.IntPtr)
168 Next i
170 ' Initialize a INTERNET_PER_CONN_OPTION_LIST instance.
171 Dim Request As New INTERNET_PER_CONN_OPTION_LIST()
173 ' Point to the allocated memory.
174 Request.pOptions = buffer
176 Request.Size = Marshal.SizeOf(Request)
178 ' IntPtr.Zero means LAN connection.
179 Request.Connection = IntPtr.Zero
181 Request.OptionCount = Options.Length
182 Request.OptionError = 0
183 Dim size As Integer = Marshal.SizeOf(Request)
185 ' Query system internet options.
186 Dim result As Boolean = NativeMethods.InternetQueryOption(IntPtr.Zero, INTERNET_OPTION.INTERNET_OPTION_PER_CONNECTION_OPTION, Request, size)
188 If Not result Then
189 Throw New ApplicationException("Get System Internet Option Failed! ")
190 End If
192 Return Request
193 End Function
195 ''' <summary>
196 ''' Restore to the system proxy settings.
197 ''' </summary>
198 Public Shared Function RestoreSystemProxy() As Boolean
199 Return RestoreSystemProxy(_agent)
200 End Function
202 ''' <summary>
203 ''' Restore to the system proxy settings.
204 ''' </summary>
205 Public Shared Function RestoreSystemProxy(ByVal agentName As String) As Boolean
206 If String.IsNullOrEmpty(agentName) Then
207 Throw New ArgumentNullException("Agent name cannot be null or empty!")
208 End If
210 Dim hInternet As IntPtr = IntPtr.Zero
212 If Not String.IsNullOrEmpty(agentName) Then
213 hInternet = NativeMethods.InternetOpen(agentName, CInt(Fix(INTERNET_OPEN_TYPE.INTERNET_OPEN_TYPE_DIRECT)), Nothing, Nothing, 0)
214 End If
216 Return RestoreSystemProxyInternal(hInternet)
217 Finally
218 If hInternet <> IntPtr.Zero Then
219 NativeMethods.InternetCloseHandle(hInternet)
220 End If
221 End Try
222 End Function
224 ''' <summary>
225 ''' Restore to the system proxy settings.
226 ''' </summary>
227 Shared Function RestoreSystemProxyInternal(ByVal hInternet As IntPtr) As Boolean
228 Dim request = GetSystemProxy()
230 Dim size As Integer = Marshal.SizeOf(request)
232 ' Allocate memory.
233 Dim intptrStruct As IntPtr = Marshal.AllocCoTaskMem(size)
235 ' Convert structure to IntPtr
236 Marshal.StructureToPtr(request, intptrStruct, True)
238 ' Set internet options.
239 Dim bReturn As Boolean = NativeMethods.InternetSetOption(hInternet, INTERNET_OPTION.INTERNET_OPTION_PER_CONNECTION_OPTION, intptrStruct, size)
241 ' Free the allocated memory.
242 Marshal.FreeCoTaskMem(request.pOptions)
243 Marshal.FreeCoTaskMem(intptrStruct)
245 If Not bReturn Then
246 Throw New ApplicationException(" Set Internet Option Failed! ")
247 End If
249 ' Notify the system that the registry settings have been changed and cause
250 ' the proxy data to be reread from the registry for a handle.
251 NativeMethods.InternetSetOption(hInternet, INTERNET_OPTION.INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0)
253 NativeMethods.InternetSetOption(hInternet, INTERNET_OPTION.INTERNET_OPTION_REFRESH, IntPtr.Zero, 0)
254 Return bReturn
255 End Function
257 End Class